<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Facilitate the collection of a time and date with dropmenus
 * @author Anthony Gallon
 * @package Antz
 */

/**
 * Facilitate the collection of a time and date with dropmenus
 * @author Anthony Gallon
 * @package Antz
 */

class JFX_Form_Components_TimeDateSelect extends JFX_Form_Component
{
    /**
     * Contains the options
     * @var array $options
     */
    protected $options = array();

    /**
     * Define the type of this object
     * @var string $type
     */
    protected $type = 'TimeDateSelect';

    /**
     * Set our initial values
     * @param string $name
     * @param string $label
     * @param array $value
     * @param int $tstamp
     */
    public function __construct($name, $label, $value='', $tstamp=''){
        $this->id = 'sel'.rand();
        if($tstamp == '') $tstamp = time();
        $this->name = $name;
        $this->label = $label;
        if($value=='') $value = array('month'=>date('n', $tstamp), 'year'=>date('Y', $tstamp), 'day'=>date('j', $tstamp), 'hour'=>date('H', $tstamp), 'minute'=>date('i', $tstamp));

        $this->value = $value;
        $this->setLang('en');
        $timeVals = array(
            'hour'=>$value['hour'],
            'minute'=>$value['minute']
        );
        $dateVals = array(
            'month'=>$value['month'],
            'year'=>$value['year'],
            'day'=>$value['day']
        );
        $this->timeSel = new JFX_Form_Components_TimeSelect($name, $label, $timeVals);
        $this->dateSel = new JFX_Form_Components_DateSelect($name, $label, $dateVals);
    }

    /**
     * Set our language which is used to make our month titles
     * @param string $lang
     */
    public function setLang($lang='en'){
        $lang = 'en';
        switch($lang){
            case 'en':

            break;
        }
    }

    /**
     * Fetch the submitted data as a unix timestamp
     * @param string $key
     * @return int $timestamp
     */
    public function fetchUnixTime($key){
        $mth = (int) post($key.'Month');
        $y = (int) post($key.'Year');
        $d = (int) post($key.'Day');
        $h = (int) post($key.'Hour');
        $min = (int) post($key.'Minute');
        $tstamp = mktime($h, $min, 0, $mth, $d, $y);
        return $tstamp;
    }

    /**
     * Get the form element as HTML text
     * @global object $gCms
     * @param bool $wrap
     * @param bool $label
     * @return string $html
     */
    public function render($wrap=true, $label=true){
        // generate the month options
        $CONFIG = JFX::registry('config');
     	if($this->required) $this->aftertext = '<span class="required"><img src="'.$CONFIG->baseUrl.'/jfx/images/required.gif" alt="Required" /></span>'.$this->aftertext;

        $this->timeSel->showLabel(false);
        $this->timeSel->setWrap(false);
        $this->dateSel->showLabel(false);
        $this->dateSel->setWrap(false);
        $returnString = $this->timeSel->render().$this->dateSel->render().'<div class="aftertext">'.$this->aftertext.'</div>';
        if($label) $returnString = '<label>'.$this->label.'</label>'.$returnString;
        if($wrap) return $this->wrap($returnString);
        else return $returnString;
    }
}
